/*
 * 品牌创建
 * @Author: yudong
 * @Date: 2018-04-19 17:53:51
 * @Last Modified by: ljb
 * @Last Modified time: 2019-06-18 14:51:23
 */
<style lang="less" rel="stylesheet/less" type="text/less" scoped>

    .v-customer-product-brand-create {
        min-height: 74vh;
    }

</style>
<template>
	<div class="v-customer-product-brand-create">
		<Row
			type="flex"
			justify="start">
			<i-col push="1">
				<Form
					ref="formValidate"
					:model="formValidate"
					:rules="ruleValidate"
					:label-width="labelWidth(80,120)">

					<Form-item
						:label="$t('brand.name')"
						prop="name">
						<i-input
							v-model="formValidate.name"
							:placeholder="$t('brand.please_enter_name')"
							type="text"/>
					</Form-item>

					<!-- <Form-item
						:label="$t('brand.url')"
						prop="url">
						<i-input
							v-model="formValidate.url"
							:placeholder="$t('brand.please_enter_url')"
							type="url"/>
					</Form-item> -->

					<!-- 排序 -->
					<Form-item
						:label="$t('common.sort')"
						prop="sort">
						<InputNumber
							:max="100000"
							:min="0"
							v-model="formValidate.sort"
							:placeholder="$t('common.placeholder',{'field':$t('common.sort')})"/>
					</Form-item>

					<Form-item
						:label="$t('brand.logo')"
						prop="logo">
						<Uploader
							ref="logo"
							@on-remove="(file)=>{handleRemove('logo',file)}"
							@on-change="(file)=>{handleChange('logo',file)}">
							{{ $t('common.pic_tip') }}
						</Uploader>
					</Form-item>

					<Form-item>
						<Button
							type="primary"
							@click="handleSubmit('formValidate')">
							{{ $t('common.submit') }}
						</Button>
						<Button
							type="ghost"
							style="margin-left: 8px"
							@click="handleReset('formValidate')">
							{{ $t('common.reset') }}
						</Button>
					</Form-item>

				</Form>
			</i-col>
		</Row>
	</div>
</template>

<script>

import { validateImage } from 'src/validate'; // 引入校验文件
import { showLocalImage } from 'src/views/mixins/showLocalImage'; // 引入公共的mixins
import labelWidthMixin from 'src/views/mixins/labelWidthMixin';
import Uploader from 'src/views/component/Uploader';

export default {

	name: 'BrandCreate',

	components: {
		Uploader,
	},

	mixins: [showLocalImage, labelWidthMixin],

	data() {
		return {
			formValidate: {
				name: '',

				// url: '',
				sort: 0, // 优先级
				logo: '',
			}, // 新建品牌时的表单
			ruleValidate: { // 表单验证规则
				name: [
					{
						required: true,
						message: this.$t('validate.required', { field: this.$t('brand.name') }),
						trigger: 'blur,change',
					},
				],
				// url: [
				// 	// { required: true, message: '品牌网址不能为空', trigger: 'blur,change' },
				// 	{
				// 		type: 'url',
				// 		message: this.$t('validate.url', { field: this.$t('brand.url') }),
				// 		trigger: 'blur,change',
				// 	},
				// ],
				sort: [
					{
						required: true,
						type: 'number',
						message: this.$t('validate.required', { field: this.$t('common.sort') }),
						trigger: 'blur,change',
					},
				],
				logo: [
					{ required: true, validator: validateImage, trigger: 'blur,change' },
				],
			},
		};
	},
	methods: {

		/**
		 * 选择图片
		 * @param {Object} file {选择的文件对象集合}
		 * @param {String} field {icon}
		 * @return {undefined}
		 */
		handleChange(field, { file }) {
			this.formValidate[field] = file;
			this.$refs.formValidate.validateField(field);
		},

		/**
		 * 删除图片
		 * @param {String} filed {icon}
		 * @return {undefined}
		 */
		handleRemove(field) {
			this.formValidate[field] = null;
			this.$refs.formValidate.validateField(field);
		},

		/**
         * 提交表单
         * @param  {string} name {表单的ref}
         * @return {undefined}
         */
		handleSubmit(name) {
			this.$refs[name].validate((valid) => {
				if (valid) {
					this.$emit('post', this.formDataCreate());
				} else {
					this.$Message.error(this.$t('common.exist_empty_field'));
				}
			});
		},

		/**
         * 重置表单
         * @param  {string} name {表单的ref}
         * @return {undefined}
         */
		handleReset(name) {
			this.$refs.logo.reset();

			this.$refs[name].resetFields();

			this.formValidate.preview_img = '';
		},

		/**
         * 手动拼接formdata数据
         * @return {Object} {拼接好的formdata数据}
         */
		formDataCreate() {
			const new_object = {
				name: this.formValidate.name,

				// url: this.formValidate.url,
				sort: this.formValidate.sort,
				logo: this.formValidate.logo,
			};
			console.log(this.formValidate.sort);
			const form_data = new FormData();

			Object.keys(new_object).forEach((key) => {
				form_data.append(key, new_object[key]);
			});

			return form_data;
		},

	},
};
</script>
